﻿

///pageURL页面路径
function goTo(pageURL) {
    //pageURL页面路径
    window.location.href = pageURL;
}

/**
 * 自定义静态消息提示框
 * @param msg 提示消息
  */
var AlertMsg = function () {
    var timeout001 = null;
    /**
     * 静态提示框
     * @param msg 提示消息
     * @param time 提示显示存在时间（毫秒）,默认2000毫秒
     */
    var showMsg = function showMsg(msg, time) {
        msg = msg + "";
        if (msg == undefined || msg == '') {
            return;
        }
        if (time == undefined || time == '' || time == 0) {
            time = 2000;
        }
        qudiao234();
        var arr = [];
        arr.push('<div id="zdydivmsg234" style="position: fixed; top: 30%;z-index: 20000;max-width:80%;border-radius:6px; ">');
        arr.push('<div style="text-align:center;word-wrap:break-word;word-break:break-all;font-size: 14px; font-family:' + '\'宋体\'' + ';background: #3F3F3F;padding:10px 20px; color: white;">');
        arr.push(msg);
        arr.push('</div></div>');
        var html = arr.join('');
        $("body").append(html);
        var widthWindow = $(window).width();
        var spaceWidth = widthWindow - $('#zdydivmsg234').outerWidth();
        var marginleft = spaceWidth / 2;
        $("#zdydivmsg234").css("margin-left", marginleft);
        timeout001 = setTimeout(qudiao234, time);//指定几秒后去掉html
    }
    /**
     * 去掉消息框
     */
    function qudiao234() {
        clearTimeout(timeout001);
        timeout001 = null;
        $("#zdydivmsg234").remove();
    }
    return showMsg;
}();

/**
 * 自定义ajax异步请求方法
 */
var AjaxCustom = function (options) {
    /**
     * 自定义ajax跨域方法
     * @param url 请求地址
     * @param parames 发送参数
     * @param showLoading 传入遮罩层加载器，为null不显示
     * @param callBack 回调函数
     */
    SendAjax();
    function SendAjax() {
        var url = options.url;//请求可用的服务地址   
        var parames = options.parames;
        var requestType = options.type;//请求方式
        var isShowLoading = options.showLoading;//是否显示遮罩层,布尔值
        var callBack = options.callBack;
        var msg = options.loadingMsg;
        try {
            if (window.navigator.onLine == false) {
                AlertMsg("网络已断开，请检查");
                return;
            }
        } catch (e) {
            AlertMsg("检查网络出错");
        }       
        if (isShowLoading == true) {
            showLoading(msg);
        }       
        $.ajax({
            url: url,
            type: requestType,
            data: parames,
            timeout: 30000,//请求超时时间
            cache: false,//false不缓存页面
            success: function (data) {
                //去掉遮罩层
                closeLoading();
                         
                var dataObj = null;
                var isObjct = typeof(data);
                if (isObjct == 'object') {
                    dataObj = data;
                } else {
                    dataObj = JSON.parse(data);
                }                         
                if (dataObj.code == "500") {
                    var words = CryptoJS.enc.Base64.parse(dataObj.data);
                    var msg = words.toString(CryptoJS.enc.Utf8);//解码消息   
                    AlertMsg(msg,3000);
                } else if (dataObj.code == "700") {//登录状态保持超时，从新登录
                    AlertMsg(dataObj.data);
                    setTimeout(function () {
                        window.location.href = "/Login/LoginView";
                    }, 2000);                   
                }
                else {
                    callBack(dataObj);
                }
            },
            error: function (e) {
                //去掉遮罩层
                if (isShowLoading == true) {
                    closeLoading();
                }
                AlertMsg("请求出错 " + e.responseText);
            }
        });
    }
};

/**
*获取当前时间，返回格式，年月日时分秒：年月日时分秒“20151124115213”；
*格式：2015-11-24 11:53:59、2015-11-24 11:54:10 267、2015-11
*/
function getDataNow() {
    var date = new Date();
    var yyyy = date.getFullYear();//四位年份
    var month = date.getMonth() + 1;//月份 0-11
    var day = date.getDate();//日
    var HH = date.getHours();//时
    var minute = date.getMinutes();//分钟
    var second = date.getSeconds();//秒
    var milliseconds = date.getMilliseconds();//毫秒
    if (month < 10) {
        month = "0" + month;
    }
    if (day < 10) {
        day = "0" + day;
    }
    if (HH < 10) {
        HH = "0" + HH;
    }
    if (minute < 10) {
        minute = "0" + minute;
    }
    if (second < 10) {
        second = "0" + second;
    }
    var time = yyyy + "-" + month + "-" + day + " " + HH + ":" + minute + ":" + second + " " + milliseconds;
    var time1 = yyyy + "-" + month + "-" + day + " " + HH + ":" + minute + ":" + second;
    var timeTxt = yyyy + month + day + HH + minute + second;
    var ym = yyyy + "-" + month;
    var ym2 = yyyy + "年" + month + "月";
    var ym3 = yyyy + "年" + month + "月" + day + "日";
    var ym4 = yyyy + "-" + month + "-" + day;
    var hhmm = HH + ":" + minute;
    var time = {
        DateTimeSSS: time,
        TimeTxt: timeTxt,
        DateTime: time1,
        yyyyMM: ym,
        yyyyMMT: ym2,
        yyyyMMddT: ym3,
        yyyyMMdd: ym4,
        HHmm: hhmm,
        yyyy: yyyy,
        month: month,
        dd: day,
        HH: HH,
        minute: minute,
        second: second
    }
    return time;
}

//新闻页图片错误显示
function notShow(obj) {
    var imgsrc = "img/nofindImg.jpg";
    obj.src = imgsrc;
    //$(obj).removeAttr("style");
}


//显示遮罩层
function showLoading(msg) {
    if (msg==undefined||msg==null||msg=="") {
        msg = "正在加载";
    }
    $(document).find("body").append("<div id=\"zc34\" style=\"text-align:center;width:100%;height:100%;position:fixed;top:0;left:0;background: rgba(0, 0, 0, 0.3);z-index:50000;display:table;\"><div style=\"display: table-cell; vertical-align: middle;\"><div id=\"zcMsg\" style=\"width:120px;height:40px;line-height:40px;background:#FFFFFF;margin:0 auto;box-shadow: 0px 0px 2px #000;text-align:center; \"><img src=\"/Content/img/loading.gif\" style=\"margin-right:3px;position:relative;top:-2px;\" />" + msg + "</div></div></div>");
}

//关闭遮罩层
function closeLoading() {
    if ($("#zc34") != undefined && $("#zc34")!=null) {
        $("#zc34").remove();
    }    
}
